package com.kekezu.kppw.data;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;
import com.kekezu.kppw.utils.StrFormat;

@SuppressWarnings("deprecation")
public class AuthData {
	static Intent intent;
	static FinalDb db;

	/**
	 * 实名认证/公司认证
	 * 
	 * @param strName
	 * @param strNumber
	 * @param strFrontSide
	 * @param strBackDside
	 * @param strValidation
	 * @param strType
	 * @param context
	 */
	public static String[] realNameAuth(String strName, String strNumber,
			String strFrontSide, String strBackDside, String strValidation,
			String strType, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/realnameAuth";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		try {
			File file1 = new File(strFrontSide);
			File file2 = new File(strBackDside);

			MultipartEntity multipart = new MultipartEntity();
			multipart.addPart("realname",
					new StringBody(strName, Charset.forName("UTF-8")));
			multipart.addPart("card_number", new StringBody(strNumber));
			multipart.addPart("card_front_side", new FileBody(file1));
			multipart.addPart("card_back_dside", new FileBody(file2));
			multipart.addPart("type",
					new StringBody(strType, Charset.forName("UTF-8")));
			multipart.addPart("token", new StringBody(users.get(0).getToken(),
					Charset.forName("UTF-8")));

			if (strType.equals("1")) {
				File file3 = new File(strValidation);
				multipart.addPart("validation_img", new FileBody(file3));
			} else {

			}

			String strJson = httpUtils.httpPost(urlString, multipart);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public static Map<String, String> realnameAuthInfo(Context context) {
		Map<String, String> map = new HashMap<String, String>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/realnameAuthInfo?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", users.get(0).getToken());

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				map.put("realname", jsondata.getString("realname"));
				map.put("card_number", jsondata.getString("card_number"));
				map.put("status", jsondata.getString("status"));
			} else {
				Toast.makeText(context, jsonObject.getString("message"),
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 添加支付宝账号
	 * 
	 * @param strName
	 * @param strAccount
	 * @param strConfirmAccount
	 * @param context
	 */
	public static String[] addAlipay(String strName, String strAccount,
			String strConfirmAccount, Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/alipayAuth";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("alipayName", strName));
		params.add(new BasicNameValuePair("alipayAccount", strAccount));
		params.add(new BasicNameValuePair("confirmAlipayAccount",
				strConfirmAccount));
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 支付宝列表
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> alipayAuthList(
			Context context) {
		ArrayList<HashMap<String, Object>> aliList = new ArrayList<HashMap<String, Object>>();
		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/alipayList?token=" + users.get(0).getToken();
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);

			JSONObject jsonObject = new JSONObject(strJson);

			String resString = jsonObject.getString("code");
			JSONArray jsonArray = jsonObject.getJSONArray("data");
			// Log.e("rescode", resString);
			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("uid", itemJsonObject.getString("uid"));
					map.put("alipay_name",
							itemJsonObject.getString("alipay_name"));
					map.put("alipay_account", StrFormat.getStarString(
							itemJsonObject.getString("alipay_account"), 4, 0));
					map.put("pay_to_user_cash",
							itemJsonObject.getString("pay_to_user_cash"));
					map.put("user_get_cash",
							itemJsonObject.getString("user_get_cash"));

					map.put("status", itemJsonObject.getString("status"));

					if (itemJsonObject.getString("status").equals("0")) {
						map.put("status_string", "待打款");
					} else if (itemJsonObject.getString("status").equals("1")) {
						map.put("status_string", "确认金额");
					} else if (itemJsonObject.getString("status").equals("2")) {
						map.put("status_string", "已认证");
					} else if (itemJsonObject.getString("status").equals("3")) {
						map.put("status_string", "认证失败");
					}
					map.put("status", itemJsonObject.getString("status"));
					map.put("auth_time", itemJsonObject.getString("auth_time"));
					aliList.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aliList;
	}

	/**
	 * 验证支付宝认证金额
	 * 
	 * @param strCash
	 * @param strID
	 * @param context
	 */
	public static String[] verifyAlipayAuthCash(String strCash, String strID,
			Context context) {
		String str[] = new String[2];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "auth/verifyAlipayAuthCash";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));
		params.add(new BasicNameValuePair("cash", strCash));
		params.add(new BasicNameValuePair("auth_id", strID));

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			str[0] = jsonObject.getString("code");
			str[1] = jsonObject.getString("message");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
}
